import 'package:flutter/material.dart';

class CustomTextField extends StatelessWidget {
  final TextEditingController controller;
  final String? label;
  final String? hintText;
  final String? prefixText;
  final VoidCallback? linkAction;
  final String? linkActionText;
  final bool hiddenLabel;
  final bool hiddenErrorTip;
  final bool obscureText;
  final bool labelMark;
  final String? Function(String?)? validator;
  final TextInputType? keyboardType;
  final TextStyle? prefixStyle;
  final Widget? suffixIcon;
  final int? maxLines;
  final int? maxLength;
  final FocusNode? focusNode;
  final TextInputAction? textInputAction;
  final ValueChanged<String>? onFieldSubmitted;
  final bool autofocus;
  final EdgeInsetsGeometry? contentPadding;
  final InputBorder? focusedBorder;
  final InputBorder? errorBorder;

  const CustomTextField({
    super.key,
    required this.controller,
    this.label,
    this.hintText,
    this.prefixText,
    this.prefixStyle,
    this.linkAction,
    this.linkActionText,
    this.hiddenLabel = false,
    this.hiddenErrorTip = false,
    this.obscureText = false,
    this.labelMark = false,
    this.validator,
    this.keyboardType,
    this.suffixIcon,
    this.maxLines = 1,
    this.maxLength = 500,
    this.focusNode,
    this.textInputAction,
    this.onFieldSubmitted,
    this.autofocus = false,
    this.contentPadding,
    this.focusedBorder,
    this.errorBorder,
  });

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (!hiddenLabel) ...[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text.rich(
                TextSpan(
                  text: label,
                  children: [if (labelMark) TextSpan(text: ' *', style: TextStyle(color: theme.colorScheme.error))],
                ),
                style: theme.textTheme.bodyMedium?.copyWith(
                  fontWeight: FontWeight.w500,
                  color: theme.colorScheme.onSurface.withOpacity(0.8),
                ),
              ),
              if (linkAction != null && linkActionText != null)
                GestureDetector(
                  onTap: linkAction,
                  child: Text(
                    linkActionText!,
                    style: theme.textTheme.bodySmall?.copyWith(
                      color: theme.colorScheme.primary,
                      decoration: TextDecoration.underline,
                    ),
                  ),
                ),
            ],
          ),
          const SizedBox(height: 8),
        ],
        TextFormField(
          controller: controller,
          focusNode: focusNode,
          textInputAction: textInputAction,
          onFieldSubmitted: onFieldSubmitted,
          autofocus: autofocus,
          decoration: InputDecoration(
            hintText: hintText,
            hintStyle: theme.textTheme.bodyMedium?.copyWith(color: theme.colorScheme.onSurface.withOpacity(0.4)),
            border: const OutlineInputBorder(borderRadius: BorderRadius.all(Radius.circular(8))),
            enabledBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(8),
              borderSide: BorderSide(color: theme.colorScheme.outline.withOpacity(0.5)),
            ),
            focusedBorder:
                focusedBorder ??
                OutlineInputBorder(
                  borderRadius: BorderRadius.circular(8),
                  borderSide: BorderSide(color: theme.colorScheme.primary, width: 1.5),
                ),
            errorBorder:
                errorBorder ??
                OutlineInputBorder(
                  borderRadius: BorderRadius.circular(8),
                  borderSide: BorderSide(color: theme.colorScheme.error),
                ),
            errorStyle:
                hiddenErrorTip
                    ? const TextStyle(height: 0.01)
                    : theme.textTheme.bodySmall?.copyWith(color: theme.colorScheme.error, height: 1.2),
            contentPadding: contentPadding ?? EdgeInsets.symmetric(vertical: 14, horizontal: 16),
            counterText: '',
            prefixStyle: prefixStyle ?? theme.textTheme.bodyMedium,
            prefixText: prefixText != null ? '$prefixText ' : null,
            suffixIcon: suffixIcon,
            filled: true,
            fillColor: theme.colorScheme.surfaceContainerHighest.withOpacity(0.4),
          ),
          obscureText: obscureText,
          keyboardType: keyboardType,
          validator: validator,
          maxLines: obscureText ? 1 : maxLines,
          minLines: 1,
          maxLength: maxLength,
        ),
      ],
    );
  }
}
